package csl.mq.rabbitmq.receive;

import cn.hutool.core.date.DateUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Description: TODO 监听新生世界
 */
@Slf4j
@Component
@RabbitListener(queues = {"new.queue"})
public class NewMessageReceive {

    @RabbitHandler
    public void onMessage(String content, Message message, Channel channel){
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String messageId = message.getMessageProperties().getMessageId();
        log.info("new.queue监听到了消息：{}",messageId);
        try {
            channel.basicAck(deliveryTag,false);
            log.info("签收成功-{} ---- {}:{}", DateUtil.now(),messageId,content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
